var Boss = Monster.extend({
	init : function(id,config) {
		this._super(id,config);
		this.isCasting = false;
		this.isCastingSet = false;
		this.castingTime;
	},
	updateFrame : function(){
		if(this.isMoving) {
			if(this.frame >= this.sprite.numberOfFrames){
				this.frame = 0;
			}else{
				this.frame += this.speed/10;
			}		
		}
		else if(this.isCasting){
			if(this.frame >= this.spritespell.numberOfFrames){
				this.frame = 0;
			}else{
				this.frame += this.attackSpeed/10;
			}	
		}
		else {
			if(this.frame >= this.spritehit.numberOfFrames){
				this.frame = 0;
			}else{
				this.frame += this.attackSpeed/10;
			}	
		}
	},
	update : function(canvas,hero,player,container) {
		if(!this.isCastingSet) {
			var that = this;
			setInterval(function() {that.cast(hero)},this.castingTime);
			this.isCastingSet = true;
		}
		if(this.health<=0) {
			player.score+=100;
			hero.bossKilled = true;
			this.kill(container);
			return;
		}
		this.attack(hero);
		if(this.isMoving){
			this.walk(hero,canvas);
		}
		for (i in this.casts){
			this.casts[i].update(canvas.width, canvas.height, {'1':hero});
		}
		this.updateFrame();
	},
	attack : function(hero) {
		//look for attacking range
		if(this.detectHero(32,hero)&& hero.health>0) {
			this.isAttacked = false;
			this.isMoving = false;
			if(this.frame >= 3) {
				hero.health -= 0.5;
			}
		}
		else if(!this.isCasting) {
			this.isMoving = true;
		}
		//look for chasing the hero
		if((this.detectHero(80,hero) && !this.detectHero(32,hero)) || this.isAttacked) {
			if(Math.abs(this.x-hero.x)>40){
				if(this.x>hero.x) {
					this.x-=this.speed;
					this.direction = 1;
				}
				else {
					this.x+=this.speed;
					this.direction = 3;
				}
			}
			if(Math.abs(this.y-hero.y)>40){
				if(this.y>hero.y) {
					this.y-=this.speed;
					this.direction = 0;
				}
				else {
					this.y+=this.speed;
					this.direction = 2;
				}
			}
			
		}
	}
});

var Boss1 = Boss.extend({
	init : function(id,config) {
		this._super(id,config);
		this.sprite = sprites.boss1walk;
		this.spritehit = sprites.boss1hit;
		this.spritespell = sprites.boss1spell;
		this.speed = 1.2;
		this.attackSpeed = 3;
		this.isCasting = false;
		this.isCastingSet = false;
		this.health = 300;
		this.mana = 50;
		this.maxHealth = this.health;
		this.maxMana = this.mana;
		this.castingTime = 10000;
	},
	cast : function() {
		var that = this;
		this.isMoving = false;
		this.isCasting = true;
		setTimeout(function(){
			that.isMoving = true;
			that.isCasting = false;
		},2000)
		if(this.health<500 && this.mana>10) {
			this.mana-=10;
			if(this.health+100>500) {
				this.health=500;
			}
			else {
				this.health+=100;				
			}
		}
	},
	draw : function(canvas) {
		if(this.isMoving) {
			canvas.ctx.drawImage(this.sprite.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else if(this.isCasting) {
			canvas.ctx.drawImage(this.spritespell.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else {
			canvas.ctx.drawImage(this.spritehit.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);
		}
		canvas.ctx.save();
		canvas.ctx.fillStyle = "red";
		canvas.ctx.fillRect(this.x+5,this.y,50*(this.health/this.maxHealth),4);
		canvas.ctx.fillStyle = "blue";
		canvas.ctx.fillRect(this.x+5,this.y+6,50*(this.mana/this.maxMana),4);
		canvas.ctx.restore();
	}
});

var Boss2 = Boss.extend({
	init : function(id,config) {
		this._super(id,config);
		this.sprite = sprites.boss2walk;
		this.spritehit = sprites.boss2hit;
		this.spritespell = sprites.boss2spell;
		this.speed = 1.2;
		this.attackSpeed = 3;
		this.isCasting = false;
		this.isCastingSet = false;
		this.health = 200;
		this.mana = 500;
		this.maxHealth = this.health;
		this.maxMana = this.mana;
		this.casts = {};
		this.numOfCasts = 0;
		this.spellSpecs= {dx: 0, dy: 10, manaCoast: 10, h: 64, w: 64, sprite: sprites.fireball, speed: 10, castDalay: 300};
		this.castingTime = 5000;
	},
	cast : function() {
		if(this.mana>0) {
			var that = this;
			this.isMoving = false;
			this.isCasting = true;
			setTimeout(function(){
					that.isMoving = true;
					that.isCasting = false;
					that.mana-=25;					
			},2000);
			if(this.mana>=25) {
				for(var i = 0;i<4;i++) {
					this.numOfCasts ++;
					this.casts[this.numOfCasts] = new Casts(this.numOfCasts, {x: this.x + this.spellSpecs.dx, y: this.y + this.spellSpecs.dy, 
						width: this.spellSpecs.w, height: this.spellSpecs.h, sprite: this.spellSpecs.sprite, 
						direction: i, speed: this.spellSpecs.speed, container: this.casts});			
				}							
			}
		}
	},
	draw : function(canvas) {
		if(this.isMoving) {
			canvas.ctx.drawImage(this.sprite.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else if(this.isCasting) {
			canvas.ctx.drawImage(this.spritespell.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else {
			canvas.ctx.drawImage(this.spritehit.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);
		}
		for(i in this.casts) {
			this.casts[i].draw(canvas);
		}
		canvas.ctx.save();
		canvas.ctx.fillStyle = "red";
		canvas.ctx.fillRect(this.x+5,this.y,50*(this.health/this.maxHealth),4);
		canvas.ctx.fillStyle = "blue";
		canvas.ctx.fillRect(this.x+5,this.y+6,50*(this.mana/this.maxMana),4);
		canvas.ctx.restore();
	}
});

var Boss3 = Boss.extend({
	init : function(id,config) {
		this._super(id,config);
		this.sprite = sprites.boss3walk;
		this.spritehit = sprites.boss3hit;
		this.spritespell = sprites.boss3spell;
		this.speed = 1.2;
		this.attackSpeed = 3;
		this.isCasting = false;
		this.isCastingSet = false;
		this.health = 500;
		this.mana = 500;
		this.maxHealth = this.health;
		this.maxMana = this.mana;
		this.castingTime = 5000;
	},
	cast : function(hero) {
		var that = this;
		this.isMoving = false;
		this.isCasting = true;
		setTimeout(function(){
			that.isMoving = true;
			that.isCasting = false;
			that.mana-=50;
		},3000);
		if(this.detectHero(200,hero)) {
			hero.health-=10;
		}
	},
	draw : function(canvas) {
		if(this.isMoving) {
			canvas.ctx.drawImage(this.sprite.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else if(this.isCasting) {
			canvas.ctx.drawImage(this.spritespell.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);
		}
		else {
			canvas.ctx.drawImage(this.spritehit.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);
		}
		canvas.ctx.save();
		canvas.ctx.fillStyle = "red";
		canvas.ctx.fillRect(this.x,this.y-13,50*(this.health/this.maxHealth),4);
		canvas.ctx.fillStyle = "blue";
		canvas.ctx.fillRect(this.x,this.y-7,50*(this.mana/this.maxMana),4);
		canvas.ctx.restore();
	}
});